import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { useAppStore } from '~/stores/app.store';
import { generateBreadcrumbs } from '~/config/routes.config';

/**
 * 面包屑导航 Hook
 * 自动根据当前路由更新面包屑
 */
export const useBreadcrumb = () => {
  const location = useLocation();
  const setBreadcrumbs = useAppStore((state) => state.setBreadcrumbs);

  useEffect(() => {
    // 根据当前路径生成面包屑
    const breadcrumbs = generateBreadcrumbs(location.pathname);
    setBreadcrumbs(breadcrumbs);
  }, [location.pathname, setBreadcrumbs]);
};

